/*
 *@本JS功能：在线批阅
 *@本js作者：杨锡君
 *@编写时间：2017年11月23日
 */
$(function() {
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var $tableBox = $(".tableBox");
    var $searchBtn = $(".searchBtn");
    var $courseNames = $("#courseNames");
    var $classNames = $("#classNames");
    var $states = $("#states");
    var th = "<tr>" +
        "<th class=\"col-lg-1\">序号</th>" +
        "<th class=\"col-lg-2\">专业</th>" +
        "<th class=\"col-lg-2\">班级/申请班</th>" +
        "<th class=\"col-lg-2\">课程</th>" +
        "<th class=\"col-lg-2\">学生姓名</th>" +
        "<th class=\"col-lg-1\">得分</th>" +
        "<th class=\"col-lg-1\">状态</th>" +
        "<th class=\"col-lg-1\">操作</th>" +
        "</tr>";

    function tabelModel(opt,start) {
        var examesource = opt.examesource;
        if(examesource == 0 && opt.state == 0){
            examesource = "暂未统计";
        }
        return "<tr data-num=\"0\">" +
            "<td class=\"col-lg-1\">"+ start +"</td>" +
            "<td class=\"col-lg-2\" title=" + opt.professionName + ">" + opt.professionName + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.className + ">" + opt.className + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.courseName + ">" + opt.courseName + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.userName + ">" + opt.userName + "</td>" +
            "<td class=\"col-lg-1\" >" + examesource + "</td>" +
            "<td class=\"col-lg-1\" title=" + opt.stateName + ">" + opt.stateName + "</td>" +
            "<td class=\"col-lg-1\">" +
            "<a href=\"javascript:;\" data-id='"+ opt.onlinequestionId +"' data-classid="+ opt.classId +"  data-username="+ opt.userName +" data-classname="+ opt.className +" data-coursename="+ opt.courseName +" data-courseid="+ opt.courseId +" data-stuorteaid=" + opt.stuorteaId + "  class=\"detailBtn\">批阅</a>" +
            "</td>" +
            "</tr>";
    }
    var classMana = {
        init: function() {
            this.bindEvent();
        },
        bindEvent: function() {
            var self = this;
            self.classList();

            $searchBtn.off("click").on("click", function() {
                var classId = Number($classNames.val());
                var courseId = Number($courseNames.val());
                var state = Number($states.val());
                var jsonStr = {
                    "pageNow": 1,
                    "pageSize": 10,
                    "classId":classId,
                    "state":state
                };
                if(courseId != 0) {
                    jsonStr.courseId = courseId
                }
                self.tabelList(jsonStr);
            });
        },
        classList:function(){
            var self = this;
            $.ajax({
                type:"post",
                url:"getAllClassByTeaId",
                async:true,
                success:function(msg){
                    msg = JSON.parse(msg);
                    $classNames.html("<option value='0'>请选择</option>");
                    if(msg.classList.length !=0){
                        $.map(msg.classList,function(opt){
                            $classNames.append("<option value="+ opt.classId +">"+ opt.className +"</option>");
                        });
                    }
                    var json = {
                        "pageNow": 1,
                        "pageSize": 10,
                        "state":3
                    };
                    self.tabelList(json);
                }
            });
        },
        tabelList: function(jsons) {
            var self = this;
            var json = JSON.stringify(jsons);
            loading1.show();
            $.ajax({
                type: "post",
                url: "getAllStuExamList",
                data: {
                    "jsonStr": json
                },
                async: true,
                success: function(msg) {
                    loading1.hide();
                    msg = JSON.parse(msg);
                    $tableBox.empty();
                    if(msg.result == "success"){
                        if(msg.memList.length != 0) {
                            //列表
                            var tabel = "";
                            var start = 1;
                            $.map(msg.memList, function(opt) {
                                tabel += tabelModel(opt,start);
                                start++;
                            });
                            $tableBox.append(th + tabel);
                            self.skipEvent();

                        } else {
                            var trImg = "<tr><td colspan='8'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $tableBox.append(th + trImg)
                        }
                        var sumpage = msg.totalPage;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = msg.pageNow;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {
                                nowpage = p;
                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNow = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "post",
                                    url: "getAllStuExamList",
                                    data: {
                                        "jsonStr": JSON.stringify(jsonStr)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        msg = JSON.parse(msg);
                                        loading1.hide();
                                        //列表
                                        $tableBox.empty();
                                        var tabel = "";
                                        start = (nowpage -1)*10+1;
                                        $.map(msg.memList, function(opt) {
                                            tabel += tabelModel(opt,start);
                                            start++;
                                        });
                                        $tableBox.append(th + tabel);

                                        self.skipEvent();

                                    }
                                });
                            }
                        });
                        //获取课程
                        $courseNames.html("<option value=\"0\">请选择</option>");
                        if(msg.courseList.length!=0){
                            $.map(msg.courseList, function(opt) {
                                $courseNames.append("<option value=" + opt.courseId + ">" + opt.courseName + "</option>");
                            });
                        }
                        if(msg.searchObejct.courseId!=undefined&&msg.searchObejct.courseId!=null){
                            $courseNames.val(msg.searchObejct.courseId);
                        }else{
                            $courseNames.val(0);
                        }
                    }else{
                        if(msg.failMsg != undefined){
                            if(msg.failMsg == "暂无权限"){
                                parent.location.reload(true);
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }else{
                            if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                Alert1.content = msg.errorMsg;
                            } else {
                                Alert1.content = "获取失败";
                            }
                            Alert1.init();
                            Alert1.show();
                        }
                    }

                }
            });

        },
        skipEvent:function(){
//			批阅
            $(".detailBtn").click(function(){
                var jsonStr = JSON.stringify({"courseId":Number($(this).attr("data-courseid"))});
                var that = $(this);
                $.ajax({
                    url:"isSetOnlineExamGrade",
                    method:"post",
                    data:{
                        "jsonStr":jsonStr
                    },
                    success:function (msg) {
                        msg = JSON.parse(msg);
                        if(msg.result == "success"){
                            var json = JSON.stringify({
                                "pageName":"testReviewDetail",
                                "paramData":{
                                    "courseId":Number(that.attr("data-courseid")),
                                    "courseName":that.attr("data-coursename"),
                                    "stuorteaId":that.attr("data-stuorteaid"),
                                    "className":that.attr("data-classname"),
                                    "userName":that.attr("data-username"),
                                    "classId":Number(that.attr("data-classid")),
                                    "id":Number(that.attr("data-id"))
                                }
                            });
                            window.location.href = "skipPage?jsonStr="+json;
                        }else{
                            Alert1.content =msg.failMsg;
                            if(msg.failMsg == "暂无权限"){
                                Alert1.success = function () {
                                    window.location.reload(true);
                                };
                            }
                            Alert1.init();
                            Alert1.show();
                        }
                    }
                });

            });
        }
    };
    classMana.init();
});